<?php
class My_Service_Export{
    public function exportAction($exportName, $exportTitle, $exportData, $helper_time){
        if(!is_array($exportData) && (is_array($exportData) || count($exportData) == 0)){
            $this->_helper->redirector('index','index');
            exit;
        }
        
//	    Zend_Layout::getMvcInstance()->disableLayout();
//	    $this->_helper->viewRenderer->setNoRender();
//   	    $helper_time = $this->_helper->time;
        error_reporting(E_ERROR | E_WARNING | E_PARSE);
			
        $workbook = My_Pear::getInstanceSpreadsheet();
	    $workbook->setVersion(8, 'utf-8'); 
		$worksheet1 = &$workbook->addWorksheet(substr($exportName, -21)); 
        $worksheet1->setInputEncoding('utf-8');
        $worksheet1->setColumn(0,0,6);
        $worksheet1->setColumn(1,30,12);
        
        
		//end style	
		$header = &$workbook->addFormat(); 
		$header->setBold(); 
		$header->setColor('white'); 
		$header->setFgColor('grey'); 
		$header->setHAlign('center'); 
		$header->setVAlign('center'); 
		$header->setBorder(1); 	
        $header->setTextWrap();

		$textStyle = &$workbook->addFormat(); 
		$textStyle->setAlign('vcentre');
		$textStyle->setHAlign('center'); 
		$textStyle->setVAlign('center'); 
        $textStyle->setBorder(1);
        $textStyle->setTextWrap();
		
		$numberFormat = &$workbook->addFormat();
		$numberFormat->setHAlign('right'); 
		$numberFormat->setVAlign('center'); 
		$numberFormat->setNumFormat('#,###');
        $numberFormat->setBorder(1);
        $numberFormat->setTextWrap();
		
		$numberFormat2 = &$workbook->addFormat();
		$numberFormat2->setHAlign('right'); 
		$numberFormat2->setVAlign('center'); 
        $numberFormat2->setBorder(1);
        $numberFormat2->setTextWrap();
        //title
        $worksheet1->write(0, 0, 'STT', $header);
        $worksheet1->write(0, 1, 'Ngày', $header);
        $num_column = 2;
        $num_row = 0;
        foreach($exportTitle as $row){
            $worksheet1->write($num_row, $num_column, $row, $header);
            ++$num_column;
        }
        //end title
        $stt = 1;
        $num_row = 1;
        foreach($exportData as $key=>$data){
            $worksheet1->write($num_row, 0, $stt, $textStyle);  
            $worksheet1->write($num_row, 1, $helper_time->dataToDate($key), $textStyle);  
            $num_column = 2;
            foreach($data as $key2=>$data2){
                if($data2){
                    $worksheet1->write($num_row, $num_column, $data2, $numberFormat);
                }else{
                    $worksheet1->write($num_row, $num_column, $data2, $numberFormat2);
                }
                ++$num_column;
            }
            ++$stt;
            ++$num_row;
        }
        
		$workbook->send($exportName.".xls"); 
		$workbook->close();
    }
}